package com.android.lovely.ui.dialog

import android.text.Editable
import android.text.InputType
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import com.android.lovely.R
import com.android.lovely.api.service.userService
import com.android.lovely.databinding.DialogBindEmailBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.framework.UIController.showToast
import com.android.lovely.framework.launchWithLoading
import com.android.lovely.user.UserManager
import com.kotlin.extensions.keyboard.hideKeyboard
import com.kotlin.extensions.keyboard.showKeyboard
import com.kotlin.extensions.view.ViewExt.clickable

class BindEmailDialog : BaseDialogFragment<DialogBindEmailBinding>(), (Editable?) -> Unit {
    override fun initializeViews() {
        if (!UserManager.userInfo?.email.isNullOrEmpty()) {
            binding.email.setText(UserManager.userInfo?.email.orEmpty())
            binding.password.setText("**********")
        } else {
            binding.email.post {
                binding.email.requestFocus()
                binding.email.showKeyboard()
            }
        }
        binding.bind.isEnabled = UserManager.userInfo?.email.isNullOrEmpty()
        binding.email.isEnabled = UserManager.userInfo?.email.isNullOrEmpty()
        binding.password.isEnabled = UserManager.userInfo?.email.isNullOrEmpty()
        binding.switcher.isVisible = UserManager.userInfo?.email.isNullOrEmpty()
        binding.disable.isVisible = UserManager.userInfo?.email.isNullOrEmpty().not()
        binding.close.clickable {
            binding.email.hideKeyboard()
            dismissAllowingStateLoss()
        }
        binding.switcher.clickable {
            switchPasswordMode()
        }
        binding.bind.clickable {
            bindEmail()
        }
        binding.email.doAfterTextChanged(this)
        binding.password.doAfterTextChanged(this)
    }

    private fun checkBindEnable() {
        binding.disable.isGone = !binding.email.text.isNullOrEmpty() && !binding.password.text.isNullOrEmpty()
    }

    private fun switchPasswordMode() {
        binding.switcher.isSelected = binding.switcher.isSelected.not()
        if (binding.switcher.isSelected.not()) {
            binding.password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        } else {
            binding.password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        }
        binding.password.setSelection(binding.password.text?.length ?: 0)
    }

    private fun bindEmail() {
        viewLifecycleScope.launchWithLoading {
            runCatching {
                val params = mapOf("platform" to "visitor", "email" to binding.email.text.toString(), "pwd" to binding.password.text.toString())
                userService.bindEmail(params)
                UserManager.refreshAuth()
                dismissAllowingStateLoss()
            }.onFailure {
                showToast(getString(R.string.bind_email_failed))
                it.printStackTrace()
            }
        }
    }

    override fun invoke(text: Editable?) {
        checkBindEnable()
    }
}